package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.common.util.EnumOperationType;
import com.dingreading.cloud.soms.entity.Stores;
import com.dingreading.cloud.soms.entity.table.StoresTableDef;
import com.dingreading.cloud.soms.mapper.StoresMapper;
import com.dingreading.cloud.soms.service.StoresService;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class StoresServiceImpl extends BaseServiceImpl<StoresMapper, Stores, StoresTableDef> implements StoresService {


    @Override
    protected StoresTableDef getTable() {
        return StoresTableDef.stores;
    }


    @Override
    public Stores getByUid(String uid) {
        return getMapper().selectOneByCondition(table.uid.eq(uid));
    }

    @Override
    public Stores getByCorpUidAndStoreUid(String corpUid, String storeUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(corpUid))
            condition.and(table.corpUid.eq(corpUid));
        condition.and(table.uid.eq(storeUid))
                .and(table.enabled.eq(1));
        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public List<Stores> getByCorpUidAndEnabled(String corpUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.corpUid.eq(corpUid))
                .and(table.enabled.eq(1));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.uid, table.storeName, table.corpUid)
                .from(table)
                .where(condition);

        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public List<Stores> getCorpOtherStores(String corpUid, String storeUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.corpUid.eq(corpUid))
                .and(table.uid.ne(storeUid))
                .and(table.enabled.eq(1));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.uid, table.storeName, table.corpUid)
                .from(table)
                .where(condition);

        return getMapper().selectListByQuery(wrapper);
    }

    @Override
    public List<Long> getProjectIdList(String storeUid) {
        QueryCondition condition = table.uid.eq(storeUid);
        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.projectType)
                .from(table)
                .where(condition);

        String projectType = getMapper().selectObjectByQueryAs(wrapper, String.class);

        List<Long> projectIdList = new ArrayList<>();
        if (StringUtils.isNotBlank(projectType)) {
            projectIdList = Arrays.stream(StringUtils.split(projectType, ","))
                    .map(Long::valueOf).collect(Collectors.toList());
        }
        return projectIdList;
    }

    @Override
    public List<String> listBySelfAndConsortium() {
        QueryCondition condition = table.operationType.eq(EnumOperationType.SELF.getType())
                .or(table.operationType.eq(EnumOperationType.CONSORTIUM.getType()))
                .and(table.enabled.eq(1));
        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.uid)
                .from(table)
                .where(condition);
        return getMapper().selectObjectListByQueryAs(wrapper, String.class);
    }
}
